/**
 * Created by baiguiren on 2/14/17.
 */

'use strict';

const AliDaYu = require('super-alidayu');
let config = require('../config');

const client = new AliDaYu({
  app_key: config.sms.alidayu.ALIDAYU_APP_KEY,
  secret: config.sms.alidayu.ALIDAYU_SECRETKEY,
});

/**
 let alidayuSendSMS = function(phone, template_code, msg_param, sign) {
  $config = [
    'app_key' => env('ALIDAYU_APP_KEY'),
    'app_secret' => env('ALIDAYU_SECRETKEY')
  ];
  $client = new Client(new App($config));
  $req = new AlibabaAliqinFcSmsNumSend();
  $req->setRecNum($phone)
    ->setSmsParam($msg_param)
    ->setSmsFreeSignName($sign)
    ->setSmsTemplateCode($template_code);

    return $resp = $client->execute($req);
  };
 */

// const options = {
//   sms_free_sign_name: '登录验证',
//   sms_param: {
//     code: '1234',
//     product: '一登',
//   },
//   rec_num: '13800138000',
//   sms_template_code: 'SMS_4045620',
// };

// 发送短信，callback方式调用
// client.sms(options, (err, ret) => {
//   if (err) {
//     console.log('error', err);
//   } else {
//     console.log('success', ret);
//   }
// });

let sendSms = function (phone, template_code, params, sign) {
  let options = {
    sms_free_sign_name: sign,
    sms_param: params,
    rec_num: phone,
    sms_template_code: template_code
  };
  // 发送短信，promise方式调用
  return client.sms(options);
    // .then(ret => console.log('success', ret))
    // .catch(err => console.log('error', err));
};

let verifyResult = function (send_result) {
  send_result = send_result.alibaba_aliqin_fc_sms_num_send_response || send_result.error_response;
  if (send_result.result) {
    return send_result.result.success;
  } else {
    return false;
  }
};

exports.verifyResult = verifyResult;
exports.sendSms = sendSms;